Hi, |
Re: Regular Expressions problem
Regexp gl_reModParts = regexp("^([^-]+-[^-]+-)([0-9]+)$")
// Declare constant Regexp in main context,
// since 'regexp' is EXTREMELY inefficient and wasteful
// Disect gl_reModParts:
// [^-] any char except dash
// [^-]+ One or more chars except dash
// [^-]+- One or more chars except dash, then a dash
// ([^-]+-[^-]+-) match 1, chars-dash-chars-dash
// [0-9]+ One or more digits
// ([0-9]+) match 2, one or more digits
// ("^ at start of string
// $") at end of string
bool GetModParts(string NameMod, &BasePart, &NumberPart)
{ // Split the well formed module name into its parts,
// Well Formed is:
// o exactly two dashes
// o characters before 1st dash
// o characters between dashes
// o only digits after 2nd dash
// The BasePart include everything up to 2nd dash
// The NumberPart includes the digits
// e.g. Base-Part-22 has NumberPart "22"
// If poorly formed, erase output strings and return false
if (gl_reModParts NameMod)
{ BasePart = NameMod[match 1]
NumberPart = NameMod[match 2]
return(true)
}
else
{ BasePart = ""
NumberPart = ""
return(false)
}
} // end GetModParts()
void Test_GetModParts(string NameMod)
{ string BasePart, NumberPart
bool Result = GetModParts(NameMod, BasePart, NumberPart)
print Result "\t[" BasePart "]\t#" NumberPart "\t*" NameMod "*\n"
}
Test_GetModParts("CTRS-DIA-118")
Test_GetModParts("1-2-03")
Test_GetModParts("CTRS-DIA-118x") // Bad, x at end
Test_GetModParts("CTRSDIA-118") // Bad, only one dash
Test_GetModParts("a-b-c-44") // Bad, 3 dashes
Test_GetModParts("-b-33") // Bad, nothing before dash
Test_GetModParts("a--33") // Bad, nothing between dashes
Test_GetModParts("a-b-") // Bad, no numbers
|
Re: Regular Expressions problem llandale - Fri Nov 13 12:54:21 EST 2009
Regexp gl_reModParts = regexp("^([^-]+-[^-]+-)([0-9]+)$")
// Declare constant Regexp in main context,
// since 'regexp' is EXTREMELY inefficient and wasteful
// Disect gl_reModParts:
// [^-] any char except dash
// [^-]+ One or more chars except dash
// [^-]+- One or more chars except dash, then a dash
// ([^-]+-[^-]+-) match 1, chars-dash-chars-dash
// [0-9]+ One or more digits
// ([0-9]+) match 2, one or more digits
// ("^ at start of string
// $") at end of string
bool GetModParts(string NameMod, &BasePart, &NumberPart)
{ // Split the well formed module name into its parts,
// Well Formed is:
// o exactly two dashes
// o characters before 1st dash
// o characters between dashes
// o only digits after 2nd dash
// The BasePart include everything up to 2nd dash
// The NumberPart includes the digits
// e.g. Base-Part-22 has NumberPart "22"
// If poorly formed, erase output strings and return false
if (gl_reModParts NameMod)
{ BasePart = NameMod[match 1]
NumberPart = NameMod[match 2]
return(true)
}
else
{ BasePart = ""
NumberPart = ""
return(false)
}
} // end GetModParts()
void Test_GetModParts(string NameMod)
{ string BasePart, NumberPart
bool Result = GetModParts(NameMod, BasePart, NumberPart)
print Result "\t[" BasePart "]\t#" NumberPart "\t*" NameMod "*\n"
}
Test_GetModParts("CTRS-DIA-118")
Test_GetModParts("1-2-03")
Test_GetModParts("CTRS-DIA-118x") // Bad, x at end
Test_GetModParts("CTRSDIA-118") // Bad, only one dash
Test_GetModParts("a-b-c-44") // Bad, 3 dashes
Test_GetModParts("-b-33") // Bad, nothing before dash
Test_GetModParts("a--33") // Bad, nothing between dashes
Test_GetModParts("a-b-") // Bad, no numbers
If you are using DOORS 9.1 or later the recommendation in the ref manual is to use regexp2 as in:
Regexp gl_reModParts = regexp2("^([^-]+-[^-]+-)([0-9]+)$")
|
Re: Regular Expressions problem llandale - Fri Nov 13 12:54:21 EST 2009
Regexp gl_reModParts = regexp("^([^-]+-[^-]+-)([0-9]+)$")
// Declare constant Regexp in main context,
// since 'regexp' is EXTREMELY inefficient and wasteful
// Disect gl_reModParts:
// [^-] any char except dash
// [^-]+ One or more chars except dash
// [^-]+- One or more chars except dash, then a dash
// ([^-]+-[^-]+-) match 1, chars-dash-chars-dash
// [0-9]+ One or more digits
// ([0-9]+) match 2, one or more digits
// ("^ at start of string
// $") at end of string
bool GetModParts(string NameMod, &BasePart, &NumberPart)
{ // Split the well formed module name into its parts,
// Well Formed is:
// o exactly two dashes
// o characters before 1st dash
// o characters between dashes
// o only digits after 2nd dash
// The BasePart include everything up to 2nd dash
// The NumberPart includes the digits
// e.g. Base-Part-22 has NumberPart "22"
// If poorly formed, erase output strings and return false
if (gl_reModParts NameMod)
{ BasePart = NameMod[match 1]
NumberPart = NameMod[match 2]
return(true)
}
else
{ BasePart = ""
NumberPart = ""
return(false)
}
} // end GetModParts()
void Test_GetModParts(string NameMod)
{ string BasePart, NumberPart
bool Result = GetModParts(NameMod, BasePart, NumberPart)
print Result "\t[" BasePart "]\t#" NumberPart "\t*" NameMod "*\n"
}
Test_GetModParts("CTRS-DIA-118")
Test_GetModParts("1-2-03")
Test_GetModParts("CTRS-DIA-118x") // Bad, x at end
Test_GetModParts("CTRSDIA-118") // Bad, only one dash
Test_GetModParts("a-b-c-44") // Bad, 3 dashes
Test_GetModParts("-b-33") // Bad, nothing before dash
Test_GetModParts("a--33") // Bad, nothing between dashes
Test_GetModParts("a-b-") // Bad, no numbers
Regards, Manuel |
Re: Regular Expressions problem llandale - Fri Nov 13 12:54:21 EST 2009
Regexp gl_reModParts = regexp("^([^-]+-[^-]+-)([0-9]+)$")
// Declare constant Regexp in main context,
// since 'regexp' is EXTREMELY inefficient and wasteful
// Disect gl_reModParts:
// [^-] any char except dash
// [^-]+ One or more chars except dash
// [^-]+- One or more chars except dash, then a dash
// ([^-]+-[^-]+-) match 1, chars-dash-chars-dash
// [0-9]+ One or more digits
// ([0-9]+) match 2, one or more digits
// ("^ at start of string
// $") at end of string
bool GetModParts(string NameMod, &BasePart, &NumberPart)
{ // Split the well formed module name into its parts,
// Well Formed is:
// o exactly two dashes
// o characters before 1st dash
// o characters between dashes
// o only digits after 2nd dash
// The BasePart include everything up to 2nd dash
// The NumberPart includes the digits
// e.g. Base-Part-22 has NumberPart "22"
// If poorly formed, erase output strings and return false
if (gl_reModParts NameMod)
{ BasePart = NameMod[match 1]
NumberPart = NameMod[match 2]
return(true)
}
else
{ BasePart = ""
NumberPart = ""
return(false)
}
} // end GetModParts()
void Test_GetModParts(string NameMod)
{ string BasePart, NumberPart
bool Result = GetModParts(NameMod, BasePart, NumberPart)
print Result "\t[" BasePart "]\t#" NumberPart "\t*" NameMod "*\n"
}
Test_GetModParts("CTRS-DIA-118")
Test_GetModParts("1-2-03")
Test_GetModParts("CTRS-DIA-118x") // Bad, x at end
Test_GetModParts("CTRSDIA-118") // Bad, only one dash
Test_GetModParts("a-b-c-44") // Bad, 3 dashes
Test_GetModParts("-b-33") // Bad, nothing before dash
Test_GetModParts("a--33") // Bad, nothing between dashes
Test_GetModParts("a-b-") // Bad, no numbers
Or are you just going to return it false? |
Re: Regular Expressions problem SystemAdmin - Fri Jul 29 12:04:55 EDT 2011 At home but I'm pretty sure it will pass if the 1st character is a space. The code gets tricky if you must not have any white space. I'd add a separater check for that. Search for "fWhite" in the forums, surely I've posted code that does that check.
|